<template>
  <div class="topClassEditInfo">
    <div class="baseDiv" >
      <div style="width:958px;border-bottom: 1px solid #e5e5e5;height: 30px;margin-top: 15px;">
        <span style="margin-left: 20px;">货值(美元)</span>
      </div>
      <div>
        <el-form :model="addForm" :rules="rules" ref="addForm"  class="addForm">

          <div v-for="(item, index) in this.responseData.wmsWrGoodsList" :key="item.goodsId" style="width: 479px">
            <div v-if=" index % 2 === 0" style="width: 479px">
              <el-form-item :label="item.goodsName">
                <el-input v-model="item.goodsValue" @keyup.native="verifyDecimal(index,item.goodsValue)"  placeholder="输入货值" clearable></el-input>
              </el-form-item>
            </div>
            <div v-if=" index % 2!==0" style="width: 479px;margin-left: 460px;margin-top: -83px;">
              <el-form-item   :label="item.goodsName">
                <el-input v-model="item.goodsValue" @keyup.native="verifyDecimal(index,item.goodsValue)"  placeholder="输入货值" clearable></el-input>
              </el-form-item>
            </div>
          </div>
        </el-form>
      </div>
    </div>
    <file-upload :roleType="this.roleType" :fromType="2"  :headTitle="'提交抽检照片'" :fileUrlsArray="this.responseData" :headSonTitle="'支持jpg,png,gif,bmp(最多选择5张)'" @getFileUrls="getFileUrls" ></file-upload>
    <el-row class="btnFrameClass">
    <el-button class="singleBtnClass" @click="saveInstore(1)" type="primary">保存</el-button>
    <el-button class="singleBtnClass" @click="saveInstore(2)" type="primary" plain>操作记录</el-button>
    </el-row>
   <!--//操作记录组件 -->
    <pc-dialog :is-show="isOperatingRecordDialog"  title ='操作记录' @on-close="isOperatingRecordDialog = false">
      <show-operating-record   :id="this.$props.detailId"  >
      </show-operating-record>
    </pc-dialog>
  </div>
</template>
<script>
//  import maValidate from 'utils/common-method/validate/index';
  import inputCom from 'components/input/baseTitleInput.vue';
  import fileUpload from 'components/fileUpload/file.vue';
  import editPic from '../instoreAdd/editPicture.vue';
//  import {validateTips} from 'utils/tips';
  import PcDialog from 'components/dialog';
  import searchSelect from 'components/search-select/searchSelect';
  import showOperatingRecord from './operatingRecord.vue';
  export default {
    components: {
      editPic,
      inputCom,
      fileUpload,
      PcDialog,
      searchSelect,
      showOperatingRecord
    },
    props: {
      detailId: {
        type: String
      },
      version: {
        type: Number
      },
      isAdditionalInformation: {
        type: Boolean
      }
    },
    data() {
      return {
        declarationFiles: [], // 报关单数组
        inspectionFiles: [], // 商检单数组
        batchFiles: [], // 生产批次单数组
        isOperatingRecordDialog: false, // 操作记录窗口状态

        responseData: {
          batchFiles: [],
          cntrNo: '',
          declarationFiles: [],
          deliverer: '',
          hashValue: '',
          id: 0,
          inspectionFiles: [],
          ownerName: '',
          receiver: '',
          signDate: '',
          signDateDisplay: '',
          storageAddress: '',
          storageCompanyName: '',
          storageLossStandard: '',
          storageName: '',
          storageRate: '',
          storerAddress: '',
          storerName: '',
          wmsWrGoodsFileList: [{
            goodsId: '',
            goodsName: '',
            wmsWrFileList: [{
              fileClass: '1',
              fileClassDisplay: '',
              fileType: '',
              goodsId: '',
              goodsName: '',
              id: '',
              name: '',
              url: '',
              urlM: ''
            }]
          }],
          wmsWrGoodsList: [{
            countryOrigin: '',
            goodsId: '',
            goodsName: '',
            goodsValue: '',
            packageDisplay: '',
            packageId: '',
            productDate: '',
            productDateDisplay: '',
            refAvgWeight: 0,
            scanWeight: 0,
            totalWeight: 0,
            unit: '',
            unitDisplay: '',
            vendor: '',
            wrGoodsId: 0,
            wrId: 0,
            wsStockNum: '0'
          }],
          wmsWrTraceFileList: [{
            fileClass: '',
            fileClassDisplay: '',
            wmsWrFileList: [{
              fileClass: '',
              fileClassDisplay: '',
              fileType: '',
              goodsId: '',
              goodsName: '',
              id: '',
              name: '',
              url: '',
              urlM: ''
            }]
          }],
          wrNo: '',
          wrStandard: ''
        },
        addForm: {
          goodsName: ''
        },
        rules: {
        }
      };
    },
    created() {
      this.roleType = parseInt(this.$store.getters.getRoleId);
      this.getDetailInfo(); // 获取单位包装
    },
    methods: {
      getDetailInfo() {
        let valueStr = '/' + this.$props.detailId + '/' + this.$props.version;
        this.ajax({
          type: 'GET',
          url: '/maliwms/wr/wrDetail' + valueStr,
          loading: true,
          success: (res) => {
            setTimeout(() => {
              this.$pcNProgress.done();
            }, 300);
            this.responseData = res.result;
            this.responseData.wmsWrGoodsFileList.forEach((item, index) => {
              if (item.wmsWrFileList == null) {
                item.wmsWrFileList = [];
              }
            });
            if (this.responseData.declarationFiles === null) {
              this.responseData.declarationFiles = [];
            }
            if (this.responseData.inspectionFiles === null) {
              this.responseData.inspectionFiles = [];
            }
            if (this.responseData.batchFiles === null) {
              this.responseData.batchFiles = [];
            }
          }
        });
      },
        // 校验整数
      verifyDecimal(index, val) {
        let value = '';
        value = val.replace(/[^\d.]/g, '');  // 清除“数字”和“.”以外的字符
        value = val.replace(/\.{2,}/g, '.'); // 只保留第一个. 清除多余的
        value = val.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
        value = val.replace(/^(-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); // 只能输入两个小数
        if (value.indexOf('.') < 0 && value !== '') { // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
          value = parseFloat(value);
        }
        if (val.startsWith('.')) { // 如果是以小数点开始则在小数点前自动补0
          value = '0' + value;
        }
        this.responseData.wmsWrGoodsList[index].goodsValue = value;
      },
      // 数组元素去重
      unique(arr) {
        let hash = [];
        for (let i = 0; i < arr.length; i++) {
          if (arr.indexOf(arr[i]) === arr.lastIndexOf(arr[i])) {
            hash.push(arr[i]);
          }
        }
        return hash;
      },
      isInArray (arr, value) {
        for (let i = 0; i < arr.length; i++) {
          if (value === arr[i]) {
            return true;
          }
        }
        return false;
      },
      getFileUrls(titleImg, fileNameArr, fileUrlArr) {
        this.responseData.wmsWrGoodsFileList.forEach((item, index) => {
          if (titleImg === item.goodsName) {
            item.wmsWrFileList = [];
            for (let i = 0; i < fileUrlArr.length; i++) {
              let singleImg = {name: '', url: ''};
              singleImg.name = fileNameArr[i];
              singleImg.url = fileUrlArr[i];
              item.wmsWrFileList.push(singleImg);
              this.removeOtherPara(item.wmsWrFileList);
            }
          }
        });
        if (titleImg === '报关单') {
          this.isEditDeclarationFiles = true;
          this.responseData.declarationFiles = [];
          for (let i = 0; i < fileUrlArr.length; i++) {
            let singleImg = {name: '', url: ''};
            singleImg.name = fileNameArr[i];
            singleImg.url = fileUrlArr[i];
            this.responseData.declarationFiles.push(singleImg);
          }
        }
        if (titleImg === '商检单') {
          this.isEditInspectionFiles = true;
          this.responseData.inspectionFiles = [];
          for (let i = 0; i < fileUrlArr.length; i++) {
            let singleImg = {name: '', url: ''};
            singleImg.name = fileNameArr[i];
            singleImg.url = fileUrlArr[i];
            this.responseData.inspectionFiles.push(singleImg);
          }
        }
        if (titleImg === '生产批次单') {
          this.isEditBatchFiles = true;
          this.responseData.batchFiles = [];
          for (let i = 0; i < fileUrlArr.length; i++) {
            let singleImg = {name: '', url: ''};
            singleImg.name = fileNameArr[i];
            singleImg.url = fileUrlArr[i];
            this.responseData.batchFiles.push(singleImg);
          }
        }
      },
      removeOtherPara (urlArray) {  // 移除url 数组 多出来的 uid status字段
        if (urlArray.length > 0) {
          for (let i = 0; i < urlArray.length; i++) {
            if (urlArray[i].uid !== '') {
              delete urlArray[i].uid;
            }
            if (urlArray[i].status !== '') {
              delete urlArray[i].status;
            }
          }
        }
      },
      saveInstore(operateType) {
        if (operateType === 1) {
          if (this.responseData.wmsWrGoodsFileList.length > 0) {
            this.responseData.wmsWrGoodsFileList.forEach((item, index) => {
              this.removeOtherPara(item.wmsWrFileList);
            });
          }
          this.removeOtherPara(this.responseData.declarationFiles);
          this.removeOtherPara(this.responseData.inspectionFiles);
          this.removeOtherPara(this.responseData.batchFiles);
          let wrGoodsListArr = [];
          this.responseData.wmsWrGoodsFileList.forEach((item, index) => {
            let wrGoodsList = {goodsId: item.goodsId, checkImg: item.wmsWrFileList};
            wrGoodsListArr.push(wrGoodsList);
          });
          let wrGoodsValueListArr = [];
          this.responseData.wmsWrGoodsList.forEach((item, index) => {
            let wrGoodsValueList = {wrGoodsId: item.wrGoodsId, goodsValue: item.goodsValue};
            wrGoodsValueListArr.push(wrGoodsValueList);
          });
          let declarationFilesArr = this.responseData.declarationFiles;
          let inspectionFilesArr = this.responseData.inspectionFiles;
          let batchFilesArr = this.responseData.batchFiles;
          this.ajax({
            type: 'POST',
            url: '/maliwms/wr/improveWrInfo',
            data: { batchFiles: batchFilesArr,
              declarationFiles: declarationFilesArr,
              inspectionFiles: inspectionFilesArr,
              wrGoodsList: wrGoodsListArr,
              wrGoodsValueList: wrGoodsValueListArr,
              wrId: this.$props.detailId
            },
            loading: true,
            success: (res) => {
              setTimeout(() => {
                this.$pcNProgress.done();
              }, 300);
              this.$emit('closeRefresh');
            }});
        } else {
          this.isOperatingRecordDialog = true;
          console.log('跳转操作记录');
        }
      }
    }
  };
</script>
<style scoped>
  .topClassEditInfo {
    width: 958px;
    height: auto
  }
  .baseDiv {
    display: flex;
    flex-wrap: wrap;
    width: 958px;
    height: auto;
    border: 1px solid #e5e5e5;
    background-color: white
  }

  .baseDiv .el-form-item {
    display: inline-block;
    width: 420px;
    margin-left: 40px;
  }

  .baseDiv .el-form-item .el-input {
    width: 420px;
  }

  .addForm{
    margin-top:10px;
    margin-bottom: 10px;
    height: auto;
    /*width:1000px;*/
  }

  .btnFrameClass
  {
    margin-left: 350px;
    margin-top: 40px;
    margin-bottom: 20px;
  }
  .singleBtnClass
  {
    width: 140px;
    height: 40px;
  }
  .selectclass {
    width: 35rem;
  }
</style>
